import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ConfigProvider} from "../../providers/config/config";
import {ValidateProvider} from "../../providers/validate/validate";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {StorageProvider} from "../../providers/storage/storage";
import {UtilsProvider} from '../../providers/utils/utils';

@IonicPage({
    segment: 'member/comment_ding/:amoy_id/:type',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-comment-ding',
    templateUrl: 'comment-ding.html',
})
export class CommentDingPage {
    active = false;
    page_title = '';
    page_declare = '';
    info = {
        star: 0,
        content: '',
        token: '',
        type: 'reward-boss',
        amoy_id: 0
    };

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public httpService: HttpServicesProvider,
        public storage: StorageProvider,
        public util: UtilsProvider
    ) {
        //评价方向
        this.info.type = this.navParams.get('type');
        if(this.info.type == 'reward-boss') {
            this.page_title = '评价佣丁';
            this.page_declare = '您对我的工作满意吗？';

        } else if(this.info.type == 'reward-employee') {
            this.page_title = '评价雇主';
            this.page_declare = '您的雇主怎么样？';
        }
    }
    //判断用户是否登录
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }
    userInfo = {};
    ionViewWillEnter() {
        //文章ID
        this.info.amoy_id = this.navParams.get('amoy_id');
        let userInfo = this.storage.get('userInfo');
        this.userInfo = JSON.parse(userInfo);
    }
    sub() {
        let tokenInfo = JSON.parse(this.storage.get('tokenInfo'));
        let token = tokenInfo['token'];
        let apiUrl = "/comment/add_comment_handle/token/" + token;
        if(this.info.content == '' || this.info.star == 0) {
            this.util.presentAlert('提交完成内容');
            return false;
        }
        this.httpService.requestPostData(apiUrl, this.info, data => {
            if(data['error'] == 200) {
                this.util.presentAlert('操作成功');
                setTimeout(() =>  {
                    this.navCtrl.popTo('PersonRewardPage');
                }, 500);
            } else {
                this.util.presentAlert(data.message);
            }
        })
    }

}
